package com.gking.centerSystem.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

// 独立的配置类，可以放在单独的文件中
@Configuration
@EnableAsync
@EnableCaching // 如果需要缓存功能，也可以放在这里
public class AsyncExecutorConfig {

    @Bean
    public Executor aiServiceRecordTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数
        executor.setCorePoolSize(2);
        // 设置最大线程数
        executor.setMaxPoolSize(5);
        // 设置队列容量
        executor.setQueueCapacity(100);
        // 设置空闲线程存活时间（秒）
        executor.setKeepAliveSeconds(30);
        // 设置线程名称前缀，方便调试和日志追踪
        executor.setThreadNamePrefix("async-AI-service-record-");
        // 设置优雅关闭，等待任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置等待超时时间（毫秒）
        executor.setAwaitTerminationSeconds(60);

        // 初始化执行器
        executor.initialize();

        return executor;
    }
}



